package com.shiguiwu.springmybatis.javabase.file.unzip;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 解壓
 * @author: stone
 * @date: Created by 2022/3/17 9:52
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.file.unzip
 */
@Slf4j
public class UnzipTests {

    public static void main(String[] args) {
        String path = "E:\\desktop\\desktop.zip";
        Map<String, ArrayList<String>> ha = new HashMap<>();
        getShapeFileByZip(path, ha);

        log.error("========================> ");


        //File unzip = new File(path);
        //if (FileUtil.getSuffix(path).equalsIgnoreCase("zip")) {
        //    try {
        //        unzip = ZipUtil.unzip(path,"E:\\desktop"+File.separator + "unzip", StandardCharsets.UTF_8);
        //    } catch (Exception e) {
        //        //e.printStackTrace();
        //        unzip = ZipUtil.unzip(path, "E:\\desktop"+File.separator + "unzip",Charset.forName("GBK"));
        //    }
        //}


    }

    public static void getShapeFileByZip(String path, Map<String, ArrayList<String>> map) {
        File unzip = new File(path);
        if ("zip".equalsIgnoreCase(FileUtil.getSuffix(path))) {

            try {
                unzip = ZipUtil.unzip(path, StandardCharsets.UTF_8);
            } catch (Exception e) {
                //e.printStackTrace();
                unzip = ZipUtil.unzip(path, Charset.forName("GBK"));
            }
        }

        boolean b = unzip.isDirectory();
        log.info("是否是目录 {}", b);
        if (b) {
            File[] files = unzip.listFiles();
            if (files == null || files.length == 0) {
                return;
            }

            for (File file : files) {
                if (!file.isDirectory() &&"shp".equalsIgnoreCase(FileUtil.getSuffix(file))) {
                    String fileName = unzip.getName();
                    ArrayList<String> list = map.computeIfAbsent(fileName, k -> new ArrayList<>());
                    list.add(file.getAbsolutePath());
                    continue;
                }

                if (!file.isDirectory() && StrUtil.equalsAnyIgnoreCase(FileUtil.getSuffix(file), "xls", "xlsx")) {
                    map.put("excel", CollUtil.newArrayList(file.getAbsolutePath()));
                }

                getShapeFileByZip(file.getAbsolutePath(), map);

            }
        }
        //else {
        //    if (FileUtil.getSuffix(unzip).equalsIgnoreCase("shp")) {
        //        //list.add(unzip.getAbsolutePath());
        //        String fileName = unzip.getName();
        //        ArrayList<String> list = map.computeIfAbsent(fileName, k -> new ArrayList<>());
        //        list.add(unzip.getAbsolutePath());
        //    }
        //}


    }



}
